import { createRouter, createWebHashHistory } from "vue-router";
import UserLogin from "@/views/UserManage/UserLogin.vue";
import Register from "@/views/UserManage/Register.vue";
import mainpage from "@/views/frame/mainpage.vue";
import { useUserStore } from "../store/userStore.js";

const routes = [
    {
        path: '/login',
        component: UserLogin
    },
    {
        path: "/register",
        component: Register
    }, {
        name: "mainpage",
        path: '/',
        component:mainpage,
        children :[
            {
                name: 'product',
                path: '/product',
                component:function (){
                    return import('@/views/product/Products.vue')
                }


            },{
                name: 'material',
                path: '/material',
                component:function (){
                    return import('@/views/product/Materials.vue')
                }


            }, {
                name: 'employee/info',
                path: '/employee/info',
                component: function () {
                    return import('@/views/employee/Employees.vue')
                }
            }, {
                name: 'employee/approve',
                path: '/employee/approve',
                component: function () {
                    return import('@/views/employee/Approve.vue')
                }
            }, {
                name: 'employee/schedule',
                path: '/employee/schedule',
                component: function () {
                    return import('@/views/employee/Schedule.vue')
                }
            },{
                name: 'activity',
                path: '/activity',
                component: function () {
                    return import('@/views/activity/Activities.vue')
                }
            },
            {
                name: 'order',
                path: '/order',
                component: function () {
                    return import('@/views/sales/Order.vue')
                }
            },
            {
                name: 'record',
                path: '/record',
                component: function () {
                    return import('@/views/sales/Record.vue')
                }
            },
            {
                name: 'backend',
                path: '/backend',
                component: function () {
                    return import('@/views/backend/Info.vue')
                }
            },

            {
                name: 'checkin',
                path: '/checkin',
                component: function () {
                    return import('@/views/checkin/Checkin.vue')
                }
             }, {
                name: 'leave',
                path: '/leave',
                component: function () {
                    return import('@/views/checkin/Leave.vue')
                 }
                },{
                name: 'myschedule',
                path: '/myschedule',
                component: function () {
                    return import('@/views/checkin/Myschedule.vue')
                }
            }, {
                name: 'delivery/receiving',
                path: '/delivery/receiving',
                component: function () {
                    return import('@/views/checkin/Receiving.vue')
                }
                },{
                name: 'productbrowsing',
                path: '/productbrowsing',
                component: function () {
                    return import('@/views/admin/Productbrowsing.vue')
                }

             },{
                name: 'manage/staff',
                path: '/manage/staff',
                component: function () {
                    return import('@/views/manage/Staff.vue')
                }

            },{
                name: 'show',
                path: '/show',
                component: function () {
                    return import('@/views/show/Show.vue')
                }
        },{
                name: 'showvue',
                path: '/showvue',
                component: function () {
                    return import('@/views/show/ShowVue.vue')
                }
            } ,{
                name: 'operationlog',
                path: '/operationlog',
                component: function () {
                    return import('@/views/Operation/Log.vue')
                }
            },{
                path:'/screen',
                component:()=>import('@/views/sales/ScreenCalling.vue')
            },{
                path:'/member',
                component:()=>import('@/views/member/member.vue')
            },{
                path:'/exceptionHandle',
                component:()=>import('@/views/exception/exceptionHandle.vue')
            },{
                name: 'abnormal-upload', // 新增name，方便跳转
                path: '/abnormal-upload', // 路径与跳转一致
                component:()=>import('@/views/exception/exceptionUpload.vue')
            },{
                path: '/Order-sales-amount',
                component:()=>import('@/views/show/sales-amount.vue')
            },{
                path: '/commodity-sales-volume',
                component:()=>import('@/views/show/sales-volume.vue')
            },{
                path: '/attendance-statistics',
                component:()=>import('@/views/show/attendance-statistics.vue')
            }

        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

const whiteList = ['/login', '/register'];
router.beforeEach(
    (to, from, next) => {
        const path = to.path;
        const userStore = useUserStore();

        if (whiteList.includes(path)) {
            console.log("路由守卫：白名单直接跳转");
            next();
            return true;
        }

        // 修改此处，去掉括号
        if (userStore.getToken) {
            console.log("路由守卫：通过");
            next();
            return true;
        } else {
            console.log("路由守卫：未登录");
            next("/login");
            return false;
        }
    }
);

export default router;